package com.cn.server.module.player.dao;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import com.cn.server.module.player.dao.entity.Player;

/**
 * 玩家dao
 *
 * @author -jiawuwei-
 */

@Component
public class PlayerDao {
    private final AtomicInteger index = new AtomicInteger(100);
    Map<String, Player> playerMapByName = new HashMap<String, Player>();
    Map<Long, Player> playerMapById = new HashMap<Long, Player>();

    public PlayerDao() {
        /*Player player1 = new Player(1, "鲁班", "root", 1, 100);
        Player player2 = new Player(2, "李元芳", "root", 2, 100);
        Player player3 = new Player(3, "女娲", "root", 3, 100);
        playerMapById.put(1,player1);
        playerMapByName.put("鲁班",player1);
        playerMapById.put(2,player1);
        playerMapByName.put("李元芳",player2);
        playerMapById.put(3,player1);
        playerMapByName.put("女娲",player3);*/
    }

    /**
     * 获取玩家通过id
     *
     * @param playerId
     * @return
     */
    public Player getPlayerById(long playerId) {
        Player player = playerMapById.get(playerId);
        return player;
    }


    /**
     * 获取玩家通过玩家名
     *
     * @param playerName
     * @return
     */
    public Player getPlayerByName(final String playerName) {
        Player player = playerMapByName.get(playerName);

        return player;
    }


    /**
     * 创建玩家
     *
     * @param player
     * @return
     */
    public Player createPlayer(Player player) {
        long abs = Math.abs(index.getAndIncrement());
        playerMapById.put(abs, player);
        player.setPlayerId(abs);
        playerMapByName.put(player.getPlayerName(), player);
        return player;
    }

}
